package yang.learn.ScalaForTheImpatient

import org.junit.Test

import scala.math.sqrt

/**
  * @ClassName: ch02
  * @Description:
  * @Author: zhangyang (zhangyang04@qiyi.com)
  * @CreateDate: 2019-01-08 22:23
  */
@Test
class ch02 {


  @Test
  def s2_1(): Unit = {
    val x = -1
    val s = if (x > 0) 1 else 0
    println(s)
  }

  @Test
  def s2_3(): Unit = {
    val x = 0
    val x0 = 1
    val y = 0
    val y0 = 1
    val distance = {
      val dx = x - x0
      val dy = y - y0
      sqrt(dx * dx + dy * dy)
    }
    println(distance)
  }

  @Test
  def s2_4() = {
    val name = "zhangyang"
    println(s"my name is $name")
  }

  @Test
  def s2_6() = {
    for (i <- 1 to 10; j <- 1 until 10 if j % 2 == 0) {
      println(s"${i + j}")
    }
  }

  def sum(args: Int*) = {
    var result = 0
    for (arg <- args) result += arg
    result
  }

  @Test
  def s2_9() = {
    val result = sum(1 to 5: _*)
    println(result)
  }

  def box(s: String) {
    println(s"这是过程!------$s")
  }

  @Test
  def s2_10() = {
    box("haha")
  }

  def process(url: String) = {
    if (url.length > 10) {
      sqrt(url.length)
    }
    else throw new IllegalArgumentException("url should be longer")
  }


  @Test
  def s2_12() = {
    val result =
      try {
        process("zhangyang--zhangyang")
      }
      catch {
        case ex: IllegalArgumentException => println(ex.getMessage)
        case ex: Exception => ex.printStackTrace()
      }
    println(result)
  }
}
